<?php

use yii\helpers\Html;
use kartik\grid\GridView;
use backend\models\Country;
use backend\assets\KartikAsset;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\LinkSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->title = '外部资源库';
$this->params['breadcrumbs'][] = $this->title;

KartikAsset::register($this);
$css = <<<CSS
    .btn-primary {
        color: #fff;
        background-color: #2194f1;
        border-color: #2194f1;
    }
    .panel-primary > .panel-heading {
        color: #fff;
        background-color: #2194f1;
        border-color: #2194f1;
    }
CSS;
$this->registerCss($css);
$js = <<<JS
  $('[data-toggle="tooltip"]').tooltip()
JS;

$this->registerJs($js);
?>
<div class="link-index">

    <?php // echo $this->render('_search', ['model' => $searchModel]);
    $columns = [
        ['class' => 'yii\grid\SerialColumn'],
        'title',
        [
            'attribute' => 'url',
            'format' => 'raw',
            'value' => function ($model) {
                return Html::a(mb_strcut($model->url, 0, 30), $model->url, ['target' => '_blank']);
            }
        ],
        [
            'attribute' => 'category',
            'value' => function ($model) {
                return Yii::$app->params['link_category'][$model->category];
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => Yii::$app->params['link_category'],
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '选择类别'],
            'format' => 'raw',
        ],
        [
            'attribute' => 'desc',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->desc . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->desc, 0, 30) . "</span>";
            }
        ],
        [
            'attribute' => 'province',
            'vAlign' => 'middle',
            'hAlign' => 'middle',
            'value' => function ($model) {
                return Country::findOne($model->province)->title;
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => Country::localCity('`pid`=0 AND `order`>0'),
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '省份'],
            'format' => 'raw',
        ],
        [
            'attribute' => 'city',
            'vAlign' => 'middle',
            'value' => function ($model) {
                if ($model->city) {
                    $modelCountry = new Country();
                    $city = implode(',', $modelCountry->getProvinces("id in ($model->city)"));
                } else {
                    $city = '';
                }
                return $city;
            },
            'filterType' => GridView::FILTER_SELECT2,
            'filter' => $searchModel->province ? Country::localCity("`pid`=" . $searchModel->province) : [],
            'filterWidgetOptions' => [
                'pluginOptions' => ['allowClear' => true],
            ],
            'filterInputOptions' => ['placeholder' => '城市'],
            'format' => 'raw'

        ],
        'contact:ntext',
        [
            'attribute' => 'score',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->score . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->score, 0, 30) . "</span>";
            }
        ],
        'number',
        'contact_before',
        'contact_now',
        [
            'attribute' => 'history',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->history . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->history, 0, 30) . "</span>";
            }
        ],
        [
            'attribute' => 'remark',
            'format' => 'raw',
            'value' => function ($model) {
                return "<span title='" . $model->remark . "' data-toggle='tooltip' data-placement='top'>" . mb_strcut($model->remark, 0, 30) . "</span>";
            }
        ],
        'updated_at:date',
        ['class' => 'yii\grid\ActionColumn', 'template' => '{update} {delete}',],
    ];
    echo GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'condensed' => true,
        'hover' => true,
        'responsive' => false,
        'toolbar' => [
            [
                'content' =>
                    Html::a('<i class="glyphicon glyphicon-plus"></i>创建', ['create'], ['title' => '创建记录', 'class' => 'btn btn-primary',]) .
                    Html::a('<i class="glyphicon glyphicon-repeat"></i>重置', ['index'], ['class' => 'btn btn-default', 'title' => '重置列表'])
            ],
            '{toggleData}'
        ],
        'panel' => [
            'heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-book"></i>外部资源库</h3>',
            'type' => 'primary',
            'footer' => '',
        ],
        'columns' => $columns,

    ]); ?>
</div>
